import { sendDeviceSetting } from '@/api/iot/iotDeviceSettingApi'
import {
  successNotice
} from '@/util/common'

export function setSwitch(that, deviceCore, port, val, callback) {
  const json = {
    deviceCore: deviceCore,
    port: port,
    setting: 'setSwitch',
    param: val ? '1' : '0'
  }
  sendSetting(that, json, callback)
}

export function setReboot(that, deviceCore, port, val, callback) {
  const json = {
    deviceCore: deviceCore,
    port: port,
    setting: 'setReboot',
    param: val
  }
  sendSetting(that, json, callback)
}

export function getData(that, deviceCore, port, val, callback) {
  const json = {
    deviceCore: deviceCore,
    port: port,
    setting: 'getData',
    param: val
  }
  sendSetting(that, json, callback)
}

export function setRealTime(that, deviceCore, port, val, callback) {
  const json = {
    deviceCore: deviceCore,
    port: port,
    setting: 'setRealTime',
    param: val
  }
  sendSetting(that, json, callback)
}

export function setBrightness(that, deviceCore, port, val, callback) {
  const json = {
    deviceCore: deviceCore,
    port: port,
    setting: 'setBrightness',
    param: val
  }
  sendSetting(that, json, callback)
}

function sendSetting(that, json, callback) {
  sendDeviceSetting(json).then(res => {
    if (callback) {
      callback(res)
    }
    if (res.code === '200') {
      successNotice(that)
    }
  })
}
